import { ControlType as TYPE } from "@components";
import { Button, Space } from "antd"
import { useMainContext } from '@contexts/MainContext'
import { RULE_REQUIRED } from "@rules";
import { useState } from "react";

const selectConfig = (name, label, codeKey) => {
    return {
      name,
      label,
      codeKey,
      controlType: TYPE.SELECT,
      allowClear: true,
      showSearch: true,
      optionFilterProp: "children",
      filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase()),
    }
  }

const fields = [
    { ...selectConfig('unitCode','机构','secondUnits') }, 
    { ...selectConfig('productCode','产品类型','allRisks'), controlType: TYPE.SELECTBOTH}, 
    {
        name: 'createTime', label: '创建时间', controlType: TYPE.DATE_RANGE_PICKER,
        linkageFields: ['startDate', 'endDate'],
    },
    { name: 'status', label: '状态', controlType: TYPE.SELECT, codeKey: 'status' }
]

const GetColumns = ( edit ) => {
    const { isButtomShow } = useMainContext()

    return [
        {
            title: '机构',
            dataIndex: 'unitName',
            key: 'unitName',
            open: true,
        },
        { title: '产品类型', dataIndex: 'productTypeStr', key: 'productTypeStr', open: true },
        { title: '创建时间', dataIndex: 'createTime', key: 'createTime', open: true },
        { title: '更新时间', dataIndex: 'updateTime', key: 'updateTime', open: true },
        {
            title: "操作",
            dataIndex: 'operationCol',
            key: 'operationCol',
            open: true,
            fixed: 'right',
            render: (_, record) => {
                return (
                    <Space>
                        {isButtomShow('430403') && <Button type="primary" onClick={() => edit(record)}>编辑</Button>}
                    </Space>
                )
            }
        }
    ]
}

const GetModalFields = (modalFormRef, disabled=false) => {

    return [
        {   ...selectConfig('unitCode','机构','secondUnits'), disabled, rules: [RULE_REQUIRED] }, 
        { 
            ...selectConfig('untrueProductCodeList','产品类型', 'allRisks'),
            rules: [RULE_REQUIRED],
            controlType: TYPE.SELECTALL,
            onChange: (value, option=[]) => {
                modalFormRef.current?.setFieldValue('productCodeList', option.map(item => item.value))
                modalFormRef.current?.setFieldValue('productTypeList', option.map(item => item.children))
            },
        }, 
        { 
            options: [],
            ...selectConfig('productCodeList','产品类型'),
            selectmode: "multiple",
            mode: 'hidden'
        }, 
        {
            ...selectConfig('productTypeList','产品类型Str'),
            selectmode: "multiple",
            mode: 'hidden'
        },
        {   name: 'status', label: '状态', controlType: TYPE.SELECT, codeKey: 'status', rules: [RULE_REQUIRED] }
    ]
}

export { fields, GetColumns, GetModalFields }